package cpmake.addons.java;

import cpmake.CPMake;
import cpmake.MakeAction;
import cpmake.rules.Rule;
import cpmake.definitions.Definition;
import cpmake.CPMakeException;
import cpmake.rules.DirectoryRule;
import cpmake.rules.SimpleRule;
import cpmake.RegExFileSet;
import java.io.File;

public class JavaProgram
	{
	private boolean m_isSetup = false;
	private CPMake m_make;
	private String m_programName;
	private JavaCRule m_javaCRule;
	private JavaCRule m_testJavaCRule;
	private JarRule m_jarRule;
	
	private void init()
		{
		m_make = CPMake.getCurrentMake();
		}
	
	public JavaProgram()
			throws CPMakeException
		{
		init();
		
		try
			{
			m_programName = new File(m_make.getWorkingDirectory(), ".")
					.getCanonicalFile().getName();
			}
		catch (java.io.IOException ioe)
			{
			throw new CPMakeException(ioe);
			}
			
		
		}
		
	//---------------------------------------------------------------------------
	public JavaProgram setup()
			throws CPMakeException
		{
		/* String buildDir = "build";
		new DirectoryRule(buildDir);  //This is for auto clean */
		
		String classesDir = m_make.getProperty("cpmake.java.program.class_directory");
		String jarDir = m_make.getProperty("cpmake.java.program.jar_directory");
		DirectoryRule jarDirRule = new DirectoryRule(jarDir);
		
			
		m_javaCRule = new JavaCRule("compile", classesDir);
		m_javaCRule.addSourceDir(m_make.getProperty("cpmake.java.program.source_directory"));
		m_javaCRule.setDescription("Compile java source code");
		
		
		m_jarRule = new JarRule("jar", jarDir+"/"+m_programName+".jar");
		m_jarRule.addDepend(jarDirRule);
		m_jarRule.addDepend(m_javaCRule);
		m_jarRule.addFileSet(new RegExFileSet(classesDir, ".*\\.class").recurse());
		m_jarRule.setDescription("Create "+m_programName+" jar file");
		
		
		//String testBuildDir = buildDir+"/tests";
		String testClassDir = m_make.getProperty("cpmake.java.program.test_class_directory");
		m_testJavaCRule = new JavaCRule("compile-tests", testClassDir);
		m_testJavaCRule.addSourceDir(m_make.getProperty("cpmake.java.program.test_source_directory"));
		m_testJavaCRule.addDepend(m_javaCRule);
		m_testJavaCRule.addClasspath(m_javaCRule.getBuildDirectory());
		m_testJavaCRule.setDescription("Compile java test code");
		
		
		final String docDir = m_make.getProperty("cpmake.java.program.javadoc_directory");
		new SimpleRule("javadoc")
				.setDescription("Create source javadocs")
				.addDepend(new DirectoryRule(docDir))
				.setMakeAction(new MakeAction()
					{
					public void doMakeAction(Rule rule)
							throws CPMakeException
						{
						Definition df = m_make.getDefinition("sun_javadoc");
						df.set("public")
								.set("destination", docDir)
								.set("source", m_javaCRule.getSources());
						m_make.exec(df.getCommand());
						}
					});
		
		m_isSetup = true;
		return (this);
		}
		
	//---------------------------------------------------------------------------
	public String getProgramName() { return (m_programName); }
	
	//---------------------------------------------------------------------------
	public JavaProgram setProgramName(String programName)
		{
		m_programName = programName;
		return (this);
		}
	
	//---------------------------------------------------------------------------
	public JavaCRule getCompileRule() { return (m_javaCRule); }
	
	//---------------------------------------------------------------------------
	public JavaCRule getTestCompileRule() { return (m_testJavaCRule); }
	
	
	//---------------------------------------------------------------------------
	public JarRule getJarRule() { return (m_jarRule); }
	}
